using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace MaterialUI
{
	[ExecuteInEditMode]
	[RequireComponent(typeof(Canvas))]
	[AddComponentMenu("MaterialUI/MaterialUI Scaler", 50)]
	public class MaterialUIScaler : UIBehaviour
	{
		public delegate void ScaleFactorChangeEvent(float scaleFactor);

		public delegate void OrientationChangeEvent(Vector2 resolution);

		public ScaleFactorChangeEvent OnScaleFactorChange;

		public OrientationChangeEvent OnOrientationChange;

		private Canvas m_TargetCanvas;

		private CanvasScaler m_Scaler;

		[SerializeField]
		[Tooltip("Should the CanvasScaler's reference resolution be swapped between portrait and landscape when the screen is?")]
		private bool m_MatchOrientationToScreen = true;

		[HideInInspector]
		[SerializeField]
		private float m_LastScaleFactor;

		[HideInInspector]
		[SerializeField]
		private Vector2 m_LastResolution;

		public Canvas targetCanvas
		{
			get
			{
				if (m_TargetCanvas == null)
				{
					m_TargetCanvas = GetComponent<Canvas>();
				}
				return m_TargetCanvas;
			}
		}

		public CanvasScaler scaler
		{
			get
			{
				if (m_Scaler == null)
				{
					m_Scaler = GetComponent<CanvasScaler>();
				}
				return m_Scaler;
			}
		}

		public bool matchOrientationToScreen
		{
			get
			{
				return m_MatchOrientationToScreen;
			}
			set
			{
				m_MatchOrientationToScreen = value;
				CheckScaleFactor();
			}
		}

		private void Update()
		{
			if (Application.isPlaying)
			{
				CheckScaleFactor();
			}
		}

		private void CheckScaleFactor()
		{
			if (targetCanvas == null)
			{
				return;
			}
			if (scaler.uiScaleMode == CanvasScaler.ScaleMode.ScaleWithScreenSize && m_MatchOrientationToScreen)
			{
				bool num = Screen.width > Screen.height;
				Vector2 referenceResolution = scaler.referenceResolution;
				float x = referenceResolution.x;
				Vector2 referenceResolution2 = scaler.referenceResolution;
				if (num == x < referenceResolution2.y && (float)Screen.width == m_LastResolution.y && (float)Screen.height == m_LastResolution.x)
				{
					Vector2 referenceResolution3 = scaler.referenceResolution;
					float x2 = referenceResolution3.x;
					referenceResolution3.x = referenceResolution3.y;
					referenceResolution3.y = x2;
					scaler.referenceResolution = referenceResolution3;
					if (OnOrientationChange != null)
					{
						OnOrientationChange(new Vector2(Screen.width, Screen.height));
					}
					m_LastResolution = new Vector2(Screen.width, Screen.height);
					return;
				}
			}
			if (m_LastScaleFactor != targetCanvas.scaleFactor)
			{
				m_LastScaleFactor = targetCanvas.scaleFactor;
				if (OnScaleFactorChange != null)
				{
					OnScaleFactorChange(m_LastScaleFactor);
				}
			}
			m_LastResolution = new Vector2(Screen.width, Screen.height);
		}

		public static MaterialUIScaler GetParentScaler(Transform transform)
		{
			if (transform == null)
			{
				return null;
			}
			Transform transform2 = transform;
			MaterialUIScaler materialUIScaler = null;
			while (transform2.root != transform2)
			{
				transform2 = transform2.parent;
				materialUIScaler = transform2.GetComponent<MaterialUIScaler>();
				if (materialUIScaler != null)
				{
					break;
				}
			}
			return materialUIScaler;
		}
	}
}
